const Core = require('@alicloud/pop-core');

class AliDnsService {
  constructor() {
    this.client = new Core({
      accessKeyId: process.env.VUE_APP_ALIYUN_ACCESS_KEY_ID,
      accessKeySecret: process.env.VUE_APP_ALIYUN_ACCESS_KEY_SECRET,
      endpoint: 'https://dns.aliyuncs.com',
      apiVersion: '2015-01-09'
    });
  }

  async updateDomainRecord(domainName, recordId, ip) {
    const params = {
      RecordId: recordId,
      RR: '@', // 主机记录，根据实际需求修改
      Type: 'A',
      Value: ip
    };

    try {
      const result = await this.client.request('UpdateDomainRecord', params, {
        method: 'POST'
      });
      return result;
    } catch (error) {
      console.error('更新域名记录失败:', error);
      throw error;
    }
  }

  async getDomainRecords(domainName) {
    const params = {
      DomainName: domainName
    };

    try {
      const result = await this.client.request('DescribeDomainRecords', params, {
        method: 'POST'
      });
      return result.DomainRecords.Record;
    } catch (error) {
      console.error('获取域名记录失败:', error);
      throw error;
    }
  }
}

export default new AliDnsService(); 